package com.example.myprojectoffeixue;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Map;

public class TianjiaCJsbadapter extends BaseAdapter {
    private Context context;
    private ArrayList<SheBeiClass> sb;
    int status;
    private TextView bt;
    private TextView bt1;
    private TextView bt2;
    public LinearLayout le;
    private Map<Integer, Integer> map1;
    private Map<Integer, Integer> map2;
    int clstatus;

    public TianjiaCJsbadapter(Context context, ArrayList<SheBeiClass> sb, Map<Integer, Integer> map1, Map<Integer, Integer> map2) {
        super();
        this.context = context;
        this.sb = sb;
        this.map1 = map1;
        this.map2 = map2;
    }

    @Override
    public int getCount() {
        return sb.size();
    }

    @Override
    public Object getItem(int position) {
        return sb.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder viewHolder;
        status = 0;
        clstatus = 0;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_addcjshebei, null);
            viewHolder = new ViewHolder();
            viewHolder.tv = (TextView) convertView.findViewById(R.id.tv_listview);
            viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv_addcjsb);
            viewHolder.iv1 = (ImageView) convertView.findViewById(R.id.iv2_addcjsb);
            viewHolder.iv2 = (ImageView) convertView.findViewById(R.id.iv3_addcjsb);
            viewHolder.all_item= (LinearLayout) convertView.findViewById(R.id.all_item);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (map1.get(position) == 1 ) {

            viewHolder.iv2.setImageResource(R.drawable.anguan);

        } else if (map1.get(position) == 0 ) {
            viewHolder.iv2.setImageResource(R.drawable.ankai);

        }

        if (map2.get(position) == 0) {

            viewHolder.iv.setImageResource(R.drawable.button_kuang_weixuan);

        } else {

            viewHolder.iv.setImageResource(R.drawable.button_kuang_yixuan);
        }

        if ("0100".equals(sb.get(position).deviceId)) {
            //开关
            if (sb.get(position).deviceName==null) {
                viewHolder.tv.setText("智能开关");
            } else {

                viewHolder.tv.setText(sb.get(position).deviceName);

            }

            viewHolder.iv1.setImageResource(R.drawable.kaiguan);

        } else if ("010C".equals(sb.get(position).deviceId)) {
            if (sb.get(position).deviceName==null) {
                viewHolder.tv.setText("取电开关");

            } else {
                viewHolder.tv.setText(sb.get(position).deviceName);

            }

            viewHolder.iv1.setImageResource(R.drawable.qudiana);

        } else if ("010B".equals(sb.get(position).deviceId)||"010A".equals(sb.get(position).deviceId)) {
            if (sb.get(position).deviceName==null) {
                viewHolder.tv.setText("智能门锁");

            } else {
                viewHolder.tv.setText(sb.get(position).deviceName);

            }
            viewHolder.iv1.setImageResource(R.drawable.ms);

        } else if ("0002".equals(sb.get(position).deviceId)) {
            //窗帘
            if (sb.get(position).deviceName==null) {
                viewHolder.tv.setText("智能窗帘");



            } else {
                viewHolder.tv.setText(sb.get(position).deviceName);
            }
            viewHolder.iv1.setImageResource(R.drawable.cl2);
        } else if ("0009".equals(sb.get(position).deviceId)) {

            if (sb.get(position).deviceName==null) {
                viewHolder.tv.setText("智能插座");
            } else {
                viewHolder.tv.setText(sb.get(position).deviceName);
            }
            viewHolder.iv1.setImageResource(R.drawable.chazuo);

        }


        viewHolder.all_item.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (1 == map2.get(position)) {
                    map2.put(position, 0);
                    viewHolder.iv.setImageResource(R.drawable.button_kuang_weixuan);
                } else {
                    map2.put(position, 1);
                    viewHolder.iv.setImageResource(R.drawable.button_kuang_yixuan);
                }

            }
        });

        viewHolder.iv2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                    if (status == 0) {
                        map1.put(position, 1);
                        status = 1;
                        viewHolder.iv2.setImageResource(R.drawable.anguan);
                    } else {
                        map1.put(position, 0);
                        status = 0;
                        viewHolder.iv2.setImageResource(R.drawable.ankai);
                    }


            }
        });

        return convertView;


    }


    class ViewHolder {
        LinearLayout all_item;
        TextView tv;
        ImageView iv;
        ImageView iv1;
        ImageView iv2;
    }


}